package com.gzx.qc.modular.asr.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gzx.qc.modular.asr.entity.GzxAsrTask;
import com.gzx.qc.modular.asr.entity.GzxAsrText;
import com.gzx.qc.modular.asr.mapper.GzxAsrTaskMapper;
import com.gzx.qc.modular.asr.mapper.GzxAsrTextMapper;
import com.gzx.qc.modular.asr.service.GzxAsrDbService;
import com.gzx.qc.modular.asr.vo.GzxAsrTextVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/7/29 16:52
 */
@DS("qc")
@Service
@RequiredArgsConstructor
public class GzxAsrDbServiceImpl implements GzxAsrDbService {

    private final GzxAsrTaskMapper asrTaskMapper;

    private final GzxAsrTextMapper asrTextMapper;

    /**
     * 查询转写任务
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    public List<GzxAsrTask> selectTaskList(LambdaQueryWrapper<GzxAsrTask> lambdaQueryWrapper) {
        return asrTaskMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据ID修改
     *
     * @param asrTask
     */
    @Override
    public void updateTaskById(GzxAsrTask asrTask) {
        asrTaskMapper.updateById(asrTask);
    }

    /**
     * 修改ASR任务表
     *
     * @param lambdaUpdateWrapper
     */
    @Override
    public void updateTask(LambdaUpdateWrapper<GzxAsrTask> lambdaUpdateWrapper) {
        asrTaskMapper.update(lambdaUpdateWrapper);
    }

    /**
     * 查询ASR文字
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    public List<GzxAsrTextVo> selectVoAsrText(LambdaQueryWrapper<GzxAsrText> lambdaQueryWrapper) {
        return asrTextMapper.selectVoList(lambdaQueryWrapper);
    }

    /**
     * 批量创建ASR任务
     *
     * @param asrTaskList
     */
    @Override
    public void batchSaveTask(List<GzxAsrTask> asrTaskList) {
        String sql = asrTaskList.stream()
                .map(s -> String.format("(%s,'%s','%s')",
                        escapeSql(IdUtil.getSnowflakeNextIdStr()),
                        escapeSql(s.getCallId()),
                        escapeSql(s.getFileUrl())))
                .collect(Collectors.joining(", ")).replace("'null'", "null");
        asrTaskMapper.batchSaveTask(sql);
    }

    /**
     * 批量保存语音文本
     *
     * @param asrTextList
     */
    @Override
    public void batchSaveText(List<GzxAsrText> asrTextList) {
        String sql = asrTextList.stream()
                .map(s -> String.format("(%s, '%s', '%s', %s, '%s', %s, '%s')",
                        escapeSql(IdUtil.getSnowflakeNextIdStr()),
                        escapeSql(s.getCallId()),
                        escapeSql(s.getTaskId()),
                        escapeSql(String.valueOf(s.getSpeaker())),
                        escapeSql(s.getContent()),
                        escapeSql(String.valueOf(s.getBeginTime())),
                        escapeSql(s.getAllDetail())))
                .collect(Collectors.joining(", ")).replace("'null'", "null");
        asrTextMapper.insertBatch(sql);
    }
}
